package com.carengine.service;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Service;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

public class CvrService extends Service {

    private static final String TAG = "CvrService";
    private static final String SERVICE_NAME = "com.rk.carrecorder.CameraCrashService";
    private static final String ACTION_START = "com.rk.carrecorder.CameraCrashService.start";
    private static final String ACTION_STOP = "com.rk.carrecorder.CameraCrashService.stop";
    private ActivityManager mManager; 
    private ArrayList<RunningServiceInfo> mRunningServiceInfo; 

    private Timer mTimer;
    private TimerTask mTask; 

    private boolean mRecording; 

    @Override
    public void onCreate() {
        super.onCreate();
        mManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        Log.i(TAG, "----onCreate()----");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "----onStartCommand----");
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_START);
        filter.addAction(ACTION_STOP);
        registerReceiver(mReceive, filter);
        new Thread(new Runnable() {

            @Override
            public void run() {
                mTimer = new Timer();
                mTask = new TimerTask() {

                    @Override
                    public void run() {
                        boolean isServiceWorked = isServiceWorked(SERVICE_NAME);
                        if(!isServiceWorked && mRecording) {
                            mRecording = false;
                            Intent intent = new Intent();
                            ComponentName com = new ComponentName("com.rk.carrecorder", "com.rk.carrecorder.CameraActivity");
                            intent.setComponent(com);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("KILLED", true);
                            try {
                                startActivity(intent);
                            } catch(ActivityNotFoundException e) {
                                Log.e(TAG, "occur activity not found exception");
                            }
                        }
                    }
                };
                mTimer.schedule(mTask, 0, 1000 * 3);
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "----onBind()----");
        return null;
    }

    @Override
    public boolean stopService(Intent name) {
        Log.i(TAG, "----stopService----");
        unregisterReceiver(mReceive);
        mTimer.cancel();
        mTask.cancel();
        return super.stopService(name);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "----onDestroy----");
    }

    private boolean isServiceWorked(String serviceName) {
        mRunningServiceInfo = (ArrayList<RunningServiceInfo>) mManager.getRunningServices(Integer.MAX_VALUE);
        for(RunningServiceInfo info : mRunningServiceInfo)  {
            if(info.service.getClassName().toString().equals(serviceName)) {
                return true;
            }
        }
        return false;
    }

    private BroadcastReceiver mReceive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.e(TAG, action);
            if(action.equals(ACTION_START)) {
                mRecording = true;
            } else if(action.equals(ACTION_STOP)) {
                mRecording = false;
            }  
            Log.i(TAG, "mRecording: " + mRecording);
        }
    };
}
